package com.scoy.honeymei.activity.mall;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.adapter.tabadapter.TabFragmentAdapter;
import com.scoy.honeymei.adapter.tabadapter.TabFragmentIntAdapter;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.bean.CateBean;
import com.scoy.honeymei.fragment.mall.GoodsListFragment;
import com.scoy.honeymei.fragment.mall.GoodsSearchFragment;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.MyUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: SearchGoodsActivity$
 * @Description: 商城搜索
 * @Author: GaoZhan
 * @CreateDate: 2019/9/11/0011$ 下午 15:20$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/9/11/0011$ 下午 15:20$
 * @UpdateRemark: 更新内容
 */
public class SearchGoodsActivity extends BaseActivity {


    @BindView(R.id.title_llt)
    LinearLayout titleLlt;
    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.hm_search_et)
    EditText hmSearchEt;
    @BindView(R.id.hm_cancle_tv)
    TextView hmCancleTv;
    @BindView(R.id.hm_tocart_iv)
    ImageView hmTocartIv;
    @BindView(R.id.hm_tab)
    TabLayout hmTab;
    @BindView(R.id.hm_vp)
    ViewPager hmVp;
    @BindView(R.id.nodata_tv)
    TextView nodataTv;
    private SearchGoodsActivity mContext;
    private List<CateBean.GoodsCategoryBean> goodsCategory;
    private int stateInt;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_goods);
        ButterKnife.bind(this);
        mContext = SearchGoodsActivity.this;

        initNormal();
    }

    @Override
    public void initNormal() {
        MyUtil.setStatusBar(mContext, getWindow(), true, R.color.colorWhite);
        titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);

        hmSearchEt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (TextUtils.isEmpty(hmSearchEt.getText())) {
                    return false;
                }
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    httpCate(hmSearchEt.getText().toString());
                    nodataTv.setVisibility(View.GONE);
                }
                return false;
            }
        });

        hmSearchEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                nodataTv.setVisibility(View.VISIBLE);

                if (TextUtils.isEmpty(hmSearchEt.getText())) {
                    stateInt = 0;
                    hmCancleTv.setText("取消");

                } else {
                    if (stateInt != 1) {
                        stateInt = 1;
                        hmCancleTv.setText("搜索");
                    }
                }
            }
        });

    }

    private void initTab(List<CateBean.GoodsCategoryBean> cateBean) {
        String[] mTitles = new String[cateBean.size()];
        ArrayList<Fragment> fragments = new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            CateBean.GoodsCategoryBean goodsCategoryBean = cateBean.get(i);
            mTitles[i] = goodsCategoryBean.getName();
            fragments.add(GoodsSearchFragment.newInstance(i));
        }

        TabFragmentAdapter tabFragmentAdapter = new TabFragmentAdapter(getSupportFragmentManager(), fragments, mTitles);
        hmTab.setupWithViewPager(hmVp, false);
        hmVp.setAdapter(tabFragmentAdapter);
        hmVp.setCurrentItem(0);
    }

    @OnClick({R.id.back_iv, R.id.hm_cancle_tv, R.id.hm_tocart_iv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.hm_cancle_tv:
                if (stateInt == 1) {
                    httpCate(hmSearchEt.getText().toString());
                    nodataTv.setVisibility(View.GONE);
                } else {
                    hmSearchEt.setText("");
                    nodataTv.setVisibility(View.VISIBLE);
                    finish();
                }

                break;
            case R.id.hm_tocart_iv:
                setResult(0x871);
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case 0x871:
                setResult(0x871);
                finish();
        }
    }

    public String getSearchStr() {
        return hmSearchEt.getText().toString();
    }

    public int getSearchId(int pos) {
        return goodsCategory.get(pos).getId();
    }


    //获取分类
    private void httpCate(String searchStr) {
        HttpParams params = new HttpParams();
        params.put("search", searchStr);
        HpGo.newInstance().HttpGo(this, MyUrl.MALL_SEARCH_CATE, params, new HpCallback() {
            @Override
            public void onSuccess(String o) {
                CateBean cateBean = new Gson().fromJson((String) o, CateBean.class);
                goodsCategory = cateBean.getGoodsCategory();
                if (goodsCategory != null && goodsCategory.size() > 0) {
                    initTab(goodsCategory);
                    nodataTv.setVisibility(View.GONE);
                } else {
                    nodataTv.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onFailed(String code, String msg, String o) {
                MyUtil.mytoast(mContext, code + "," + msg);
            }

            @Override
            public void onError(int code, String errorMsg) {
                MyUtil.mytoast(mContext, errorMsg);
            }
        });

    }
}
